package com.lyf.service.admin.impl;

import com.github.pagehelper.Page;
import com.lyf.constant.MessageConstant;
import com.lyf.constant.PasswordConstant;
import com.lyf.constant.StatusConstant;
import com.lyf.context.BaseContext;
import com.lyf.dto.EmployeeDTO;
import com.lyf.dto.EmployeeLoginDTO;
import com.lyf.dto.EmployeePageQueryDTO;
import com.lyf.dto.PasswordEditDTO;
import com.lyf.pojo.Employee;
import com.lyf.exception.AccountLockedException;
import com.lyf.exception.AccountNotFoundException;
import com.lyf.exception.PasswordErrorException;
import com.lyf.mapper.admin.EmployeeMapper;
import com.lyf.service.admin.EmployeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmployeeServiceImpl  implements EmployeeService {

    @Resource
    private EmployeeMapper employeeMapper;

    /**
     * 员工登录
     *
     * @param employeeLoginDTO
     * @return
     */
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {
        String username = employeeLoginDTO.getUsername();
        String password = employeeLoginDTO.getPassword();//

        //1、根据用户名查询数据库中的数据  select * from employee where username = #{username}
        Employee employee = employeeMapper.getByUsername(username); //

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (employee == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }else if (employee.getStatus() == StatusConstant.DISABLE){
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }

        //密码比对
        // md5加密后进行密码比对
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(employee.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        if (employee.getStatus() == StatusConstant.DISABLE) {
            //账号被锁定
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }
        //3、返回实体对象
        return employee;
    }

    @Override
    public void insert(EmployeeDTO employeeDTO) {
            Employee employee = new Employee();
            // 对象属性拷贝
            BeanUtils.copyProperties(employeeDTO, employee);

            // 初始密码设置为123456，需要进行md5加密处理
            employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));

            // 设置账号状态，1表示启用，0表示禁用
            employee.setStatus(StatusConstant.ENABLE);

            // 通过AOP 自动填充功能，填充创建时间、创建时间、创建人、更新人

            employeeMapper.insert(employee);
    }


    @Override
    public Page<Employee> pageQuery(EmployeePageQueryDTO employeePageQueryDTO) {
         return employeeMapper.pageQuery(employeePageQueryDTO.getName());
    }

    @Override
    public void update(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO, employee);
        // 通过AOP 自动填充功能，更新时间、更新人
        employeeMapper.update(employee);
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        Employee employee = Employee.builder()
                .status(status)
                .id(id)
                .build();
        employeeMapper.update(employee);
    }

    @Override
    public Employee getById(Long id) {
        Employee employee =  employeeMapper.selectById(id);
        if (employee ==null){
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        return employee;
    }

    @Override
    public void editPassword(PasswordEditDTO passwordEditDTO) {
        Long empId = BaseContext.getCurrentId();
        Employee employee = employeeMapper.selectById(empId);

        // 判断旧密码是否正确
        String oldPassword = passwordEditDTO.getOldPassword();
        oldPassword = DigestUtils.md5DigestAsHex(oldPassword.getBytes());
        if (!employee.getPassword().equals(oldPassword)){
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }
        // 新密码加密
        String newPassword = passwordEditDTO.getNewPassword();
        newPassword = DigestUtils.md5DigestAsHex(newPassword.getBytes());

        employee = Employee.builder()
                .id(empId)
                .password(newPassword)
                .build();
        employeeMapper.update(employee);
    }
}